<?php

namespace App\Models;

class ProdCategory extends Base
{
    //分页获取列表数据
    public function getPageList($options=[],$where=[]){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->where($where)->orderBy('created_at','desc');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //新增单个商品分类
    public function oneInsert($data=array()){
        try {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $id = $this->insertGetId($data);
            if($id){
                return [
                    'code'=>0,
                    'data'=>$id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //编辑单个商品分类
    public function doUpdate($data){
        try {
            $now = date('Y-m-d H:i:s');
            $data['updated_at'] = $now;
            $res = $this->where('id',$data['id'])->update($data);
            if($res){
                return [
                    'code'=>0,
                    'data'=>$data['id']
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'更新失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //获取分类下的商品数据
    public function getCateProducts($cids,$lang='zn',$token){
        try {
            $user = "";
            if($token){
                $user = (new User())->where('token',$token)->first();
            }
            if(!empty($cids)){
                $cates = $this->whereIn('id',$cids)->get();
            }else{
                $cates = self::all();
            }
            $return = [];
            $catename = $lang=='zn'?'catename_zn':'catename';
            foreach ($cates as $key=>$cate){
                $prods = (new Product())->where('deleted_at',null)->whereRaw('FIND_IN_SET(?,cids)', [$cate->id])->get()->toArray();
                if($user){
                    foreach ($prods as &$prod){
                        $discounts = $this->getProdDiscount($user,$prod['id']);
                        $prod['rprice'] = $prod['rprice'] * $discounts['discount'] / 100;
                    }
                }
                $return[$key]['catename'] = $cate->$catename;
                $return[$key]['prods'] = $prods;
            }
            return [
                'code'=>0,
                'data'=>$return
            ];
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }
}
